package net.tavion.angelboot.fegin.logger;


import net.tavion.angelboot.common.utils.ReflectionUtils;
import org.apache.commons.lang3.StringUtils;

public class RequestInfo<T> {
    private String action = "request";
    private String appName;
    private String uri;
    private String loginName;
    private String token;
    private T target;

    public RequestInfo() {
    }

    public static RequestInfo.Builder builder() {
        return new RequestInfo.Builder();
    }

    public static RequestInfo.Builder stringBuilder() {
        return new RequestInfo.Builder();
    }

    public String getAction() {
        return this.action;
    }

    public void setAction(String action) {
        this.action = action;
    }

    public String getUri() {
        return this.uri;
    }

    public void setUri(String uri) {
        this.uri = uri;
    }

    public String getAppName() {
        return this.appName;
    }

    public void setAppName(String appName) {
        this.appName = appName;
    }

    public String getLoginName() {
        return this.loginName;
    }

    public void setLoginName(String loginName) {
        this.loginName = loginName;
    }

    public String getToken() {
        return this.token;
    }

    public void setToken(String token) {
        this.token = token;
    }

    public T getTarget() {
        return this.target;
    }

    public void setTarget(T target) {
        this.target = target;
    }

    public static class Builder<T> {
        private String action;
        private String uri;
        private String appName;
        private String loginName;
        private String token;
        private T target;

        public Builder() {
        }

        public RequestInfo.Builder action(String action) {
            this.action = action;
            return this;
        }

        public RequestInfo.Builder uri(String uri) {
            this.uri = uri;
            return this;
        }

        public RequestInfo.Builder appName(String appName) {
            this.appName = appName;
            return this;
        }

        public RequestInfo.Builder loginName(String loginName) {
            this.loginName = loginName;
            return this;
        }

        public RequestInfo.Builder token(String token) {
            this.token = token;
            return this;
        }

        public RequestInfo.Builder target(T target) {
            this.target = target;
            return this;
        }

        public RequestInfo<T> build() {
            RequestInfo<T> requestInfo = new RequestInfo();
            this.append(requestInfo);
            return requestInfo;
        }

        public <R extends RequestInfo> RequestInfo build(Class<R> requestInfoSubClass) {
            RequestInfo<T> requestInfo = (RequestInfo) ReflectionUtils.newInstance(requestInfoSubClass);
            this.append(requestInfo);
            return requestInfo;
        }

        private void append(RequestInfo<T> requestInfo) {
            if (StringUtils.isNotBlank(this.action)) {
                requestInfo.setAction(this.action);
            }

            requestInfo.setUri(this.uri);
            requestInfo.setAppName(this.appName);
            requestInfo.setLoginName(this.loginName);
            requestInfo.setToken(this.token);
            requestInfo.setTarget(this.target);
        }
    }
}